package com.sinosoft.workflow.util;

import java.lang.reflect.Method;
import java.util.List;

public class PrintListUtil {

	// 打印list对象
	public static <T> void printList(List<T> list) {
		if (list == null || list.size() == 0) {
			return;
		}

		for (T t : list) {
			printObject(t);
			System.out.println("-----------------------------");
		}
	}

	// 打印对象中的get方法
	public static <T> void printObject(T entity) {
		if (entity == null)
			return;
		Class<? extends Object> clzz = entity.getClass();

		Method[] methods = null;
		//只查询自己public 方法
//		methods = clzz.getDeclaredMethods();
		methods = clzz.getMethods();
		
		printMethod(entity, methods);

	}

	private static void printMethod(Object obj, Method[] methods) {
		if (methods == null || methods.length == 0) {
			return;
		}

		for (int i = 0; i < methods.length; i++) {
			Method method = methods[i];
			String temp = method.getName();
			if (temp == null || "".equals(temp) || !temp.contains("get")) {
				continue;
			}

			Class<?>[] paras = method.getParameterTypes();
			if (paras == null || paras.length > 0) {
				continue;
			}

			Class<?> returnClazz = method.getReturnType();
			if (returnClazz != String.class && returnClazz !=Integer.class) {
				continue;
			}
			
			try {
			    
				Object o = method.invoke(obj);
				String value="";
				if (o != null) {
					value = o.toString();
				}
				
				String name = temp.substring(3);
				System.out.println(name + ":" + value);
			} catch (Exception e) {
				System.out.println("出现异常");
			}
		}

	}
	public static void end(){
	    System.out.println("操作完成");
	}
}
